package com.example.tao_city.text1;

import android.content.Intent;
import android.icu.text.RelativeDateTimeFormatter;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneNumberUtils;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by TAO_CITY on 2018/1/25.
 */

public class Zhanghao_zhuche extends AppCompatActivity implements View.OnClickListener{
    private ImageView back,img;
    private boolean xuanzhong=false;
    private EditText phone,yzm;
    private Button fasong,zhuche;
    private String yanzhenma;
    private TextView xieyi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhanghaozhuche);
        initview();
    }

    private void initview() {
        back=findViewById(R.id.zhanghaozhuche_back);
        img=findViewById(R.id.zhanghaozhuche_img);
        phone=findViewById(R.id.zhanghao_phone);
        fasong=findViewById(R.id.zhanghao_fasong);
        zhuche=findViewById(R.id.zhanghao_zhuche);
        yzm=findViewById(R.id.zhanghao_yzm);
        zhuche.setEnabled(false);
        fasong.setEnabled(false);
        xieyi=findViewById(R.id.zhanghao_xieyi);

        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String ph= String.valueOf(s);
                if (isPhoneNumber(ph)) {
                    fasong.setEnabled(true);
                    fasong.setBackgroundResource(R.drawable.button);
                }else {
                    fasong.setEnabled(false);
                    fasong.setBackgroundResource(R.drawable.huibutton);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        xieyi.setOnClickListener(this);
        zhuche.setOnClickListener(this);
        fasong.setOnClickListener(this);
        back.setOnClickListener(this);
        img.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zhanghaozhuche_back:
                finish();
                break;
            case R.id.zhanghaozhuche_img:
                if(xuanzhong==false){
                    img.setImageResource(R.drawable.xuanzhong_jihuo);
                    zhuche.setBackgroundResource(R.drawable.button);
                    zhuche.setEnabled(true);
                    xuanzhong=true;
                }else {
                    img.setImageResource(R.drawable.xuanzhong_weijihuo);
                    zhuche.setBackgroundResource(R.drawable.huibutton);
                    zhuche.setEnabled(false);
                    xuanzhong=false;
                }
                break;
            case R.id.zhanghao_fasong:
                double i= (double) Math.random()*1000000;
                String a=i+"";
                yanzhenma=a.substring(0,6);
                Toast.makeText(Zhanghao_zhuche.this,yanzhenma,Toast.LENGTH_SHORT).show();
                break;
            case R.id.zhanghao_zhuche:
                String ma= String.valueOf(yzm.getText());
                if(ma.equals(yanzhenma)){
                    WindowManager.LayoutParams lp=getWindow().getAttributes();
                    lp.alpha=0.6f;
                    getWindow().setAttributes(lp);
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    MyToast.makeText(this,"恭喜你，注册成功",Toast.LENGTH_SHORT).show();
                    Handler handler=new Handler();
                    handler.postDelayed(runnable,2000);
                }else {
                    Toast.makeText(this,"验证码错误",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.zhanghao_xieyi:
                Intent in=new Intent();
                in.setClass(this,Zhuchexieyi_text.class);
                startActivity(in);
                break;
        }
    }
    public static boolean isPhoneNumber(String phoneNo) {
        if (TextUtils.isEmpty(phoneNo)) {
            return false;
        }
        if (phoneNo.length() == 11) {
            for (int i = 0; i < 11; i++) {
                if (!PhoneNumberUtils.isISODigit(phoneNo.charAt(i))) {
                    return false;
                }
            }
            Pattern p = Pattern.compile("^((13[^4,\\D])" + "|(134[^9,\\D])" +
                    "|(14[5,7])" +
                    "|(15[^4,\\D])" +
                    "|(17[3,6-8])" +
                    "|(18[0-9]))\\d{8}$");
            Matcher m = p.matcher(phoneNo);
            return m.matches();
        }
        return false;
    }
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            finish();
        }
    };


}
